package down{
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.external.ExternalInterface;
	import flash.net.navigateToURL;
	import flash.net.URLRequest;
	import ui.down.ResultUI;
	/**
	 * ...
	 * @author oppo
	 */
	public class DownResult extends ResultUI{
		/**湖北*/
		private const HUBEI:int=18;
		/**湖南*/
		private const HUNAN:int=19;
		/**江西*/
		private const JIANGXI:int=15;
		/**天津*/
		private const TIANJIN:int=03;
		/**陕西*/
		private const SHANXI:int=27;
		
		private static var _instance:DownResult
		public static function get instance():DownResult{
			if(_instance) return _instance;
			return _instance=new DownResult();
		}
		private const TIPS_X:Array=[4,61,117,175,233,288,346,404];
		private const UNLOAD_VALUE:Array=[0,64,128,256,512,1024,1280];
		private const DOWN_VALUE:Array=[0,256,512,1024,1280,2560,5120];
		private var loaders:Array;
		private var unLoaders:Array;
		private var bandInfo:Object;
		private var loadData:Object;
		private var unLoadData:Object;
		private var timeData:Object;
		
		public function start(loaders:Array,unLoaders:Array,bandInfo:Object,timeData:Object):void{
			App.scene.scene=this;
			this.upMask.visible=false;
			this.loaders=loaders;
			this.bandInfo=bandInfo;
			this.timeData=timeData;
			this.unLoaders=unLoaders;
			this.loadData={};
			this.unLoadData={};
			this.initResult();
			this.addEventListener(TextEvent.LINK,this.onTextLink);
			this.addEventListener(MouseEvent.CLICK,this.onStageClick);
			this.addEventListener(Event.REMOVED_FROM_STAGE,this.onRemoveFromStage);
			
			this.tfDown.text="提示：如需更专业更准确的宽带测速请下载<a href='event:down'><u><font color='#3577bb'>“中国电信10000管家”</font></u><a>";
		}
		private function initResult():void{
			var sum:int=0;
			var endValue:Array=[];
			for(var j:int=5;j<this.loaders[0].length;j++){
				endValue[j-5]=0;
				for (var i:int = 0; i < this.loaders.length; i++) {;
					endValue[j-5]+=this.loaders[i][j];
				}
				sum+=endValue[j-5];
			}
			endValue.sort(Array.NUMERIC);
			
			this.loadData.sum=sum;
			this.loadData.min= int(endValue[0]/1024);
			this.loadData.average=Math.round(sum/endValue.length/1024*10)/10;
			this.loadData.max= int(endValue[endValue.length - 1]/1024);
			
			if(this.bandInfo.server.prov==this.JIANGXI&&this.bandInfo.usertype=="ZX"){
				this.loadData.average*=1.12;
				this.loadData.max*=1.12
			}
			var band:Number;
			
			var showSpeed:Number;
			if(this.bandInfo.server.prov==this.HUNAN){
				showSpeed=this.loadData.max;
			}else{
				showSpeed=this.loadData.average
			}
			band=int(showSpeed/ 128 * 10) / 10;
			var factor:Number=0;
			
			if (this.bandInfo["speed"]!=0) {
				var speed:String=bandInfo["speed"];
				var value:Number=int(speed.substr(0,speed.length-1));
				var str:String=speed.substring(speed.length-1);
				if(str=="K") value/=1024;
				factor = band /value;
				
				if (factor >= 0.8) {
					this.tfEvaluation.text="很好"
				}else if (factor > 0.6) {
					this.tfEvaluation.text="一般"
				}else {
					this.tfEvaluation.text="偏慢"
				}
			}else{
				this.tfEvaluation.text="未知";
				this.tfSpeed.text = "没有取到用户宽带信息，无法评价结果"
			}
			if(factor >= 0.8&&factor<=1){
				tfSpeed.text =showSpeed + "KB/秒, "+"约为"+speed+"带宽"  ;
			}else{
				tfSpeed.text =showSpeed + "KB/秒, "+"约为"+band+"M带宽"  
			}
			this.tfAverage.text="平均值："+this.loadData.average + "KB/秒";
			this.tfPeak.text="峰　值："+this.loadData.max + "KB/秒";
			if(this.bandInfo.server.prov==this.SHANXI){
				this.mcTips2.visible=false;
				this.upMask.visible=true;
				tfSpeed.text =showSpeed + "KB/秒" ;
			}
			
			sum=0;
			this.unLoaders.sort(Array.NUMERIC);
			for(i=5;i<this.unLoaders.length;i++){
				sum+=this.unLoaders[i];
			}
			this.unLoadData.sum=sum;
			this.unLoadData.min= int(this.unLoaders[0]/1024);
			this.unLoadData.average=Math.round(sum/(this.unLoaders.length-5)/1024*10)/10;
			this.unLoadData.max= int(this.unLoaders[this.unLoaders.length - 1]/1024);
			
			this.tfUPAverage.text="平均值："+this.unLoadData.average + "KB/秒";
			this.tfUPPeak.text="峰　值："+this.unLoadData.max + "KB/秒";
			
			
			var averageValue:int=this.loadData.average;
			if(averageValue>this.DOWN_VALUE[this.DOWN_VALUE.length-1]){
				this.mcTips.x=this.TIPS_X[this.TIPS_X.length-1];
			}else{
				for(i=1;i<this.DOWN_VALUE.length;i++){
					if(averageValue>this.DOWN_VALUE[i]) continue;
					this.mcTips.x=(averageValue-this.DOWN_VALUE[i-1])/(this.DOWN_VALUE[i]-this.DOWN_VALUE[i-1])*58+this.TIPS_X[i-1];
					break;
				}
			}
			averageValue=this.unLoadData.average;
			if(averageValue>this.UNLOAD_VALUE[this.UNLOAD_VALUE.length-1]){
				this.mcTips2.x=this.TIPS_X[this.TIPS_X.length-1];
			}else{
				for(i=1;i<this.UNLOAD_VALUE.length;i++){
					if(averageValue>this.UNLOAD_VALUE[i]) continue;
					this.mcTips2.x=(averageValue-this.UNLOAD_VALUE[i-1])/(this.UNLOAD_VALUE[i]-this.UNLOAD_VALUE[i-1])*58+this.TIPS_X[i-1];
					break;
				}
			}
			if(!ExternalInterface.available) return;
			ExternalInterface.call("returnwebspeed", this.reXML().toXMLString());//函数名，参数
		}
		
		private function reXML():XML{
			var xml:XML =<root>
							<info type="20000">
								< server > {this.timeData.urlInfo}</server>
								<download>
									<averagevalue>{this.loadData.average}</averagevalue>
									<maxvalue>{this.loadData.max}</maxvalue>
									<starttime>{this.timeData.loadStart}</starttime>
									<endtime>{this.timeData.loadEnd}</endtime>
									<duration>15</duration>
									<filesize>{this.loadData.sum}</filesize>
									<evaluate>{this.tfEvaluation.text}</evaluate>
								</download>
								<upload>
									<averagevalue>{this.unLoadData.average}</averagevalue>
									<maxvalue>{this.unLoadData.max}</maxvalue>
									<starttime>{this.timeData.unLoadStart}</starttime>
									<endtime>{this.timeData.unLoadEnd}</endtime>
									<duration>15</duration>
									<filesize>{this.unLoadData.sum}</filesize>
									<evaluate>{this.tfEvaluation.text}</evaluate>
								</upload>
							</info>
						</root>
			return xml;
		}
		
		private function onStageClick(evt:MouseEvent):void{
			var name:String=evt.target.name;
			switch(name){
				case "btReTest":
					Downing.instance.start();
					break;
				case "btView":
					HuanSuan.instance.start();
					break;
			}
		}
		private function onRemoveFromStage(evt:Event):void{
			this.removeEventListener(MouseEvent.CLICK,this.onStageClick);
			this.removeEventListener(Event.REMOVED_FROM_STAGE,this.onRemoveFromStage);
		}
		private function onTextLink(evt:TextEvent):void{
			var link:String=evt.text;
			if(link!="down") return;
			
			navigateToURL(new URLRequest(this.bandInfo["server"]["exe"]), "_blank");

		}
	}
}